package com.kma.summer2012.dao;

import com.kma.summer2012.factory.HibernateUtil;
import com.kma.summer2012.model.CrumbsRequest;
import com.kma.summer2012.model.CrumbsRequestAndWork;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Администратор
 * Date: 15.01.13
 * Time: 19:00
 * To change this template use File | Settings | File Templates.
 */
public class CrumbsRequestAndWorkDAOImpl implements CrumbsRequestAndWorkDAO {
    @Override
    public void add(CrumbsRequestAndWork requestAndWork) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(requestAndWork);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public CrumbsRequestAndWork addWithReturn(CrumbsRequestAndWork requestAndWork) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(requestAndWork);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
            return requestAndWork;
        }
    }

    @Override
    public void update(CrumbsRequestAndWork requestAndWork) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(requestAndWork);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public CrumbsRequestAndWork getById(int requestAndWork_id) {
        Session session = null;
        CrumbsRequestAndWork requestAndWork = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            requestAndWork = (CrumbsRequestAndWork) session.get(CrumbsRequestAndWork.class, requestAndWork_id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return requestAndWork;
    }

    @Override
    public Collection<CrumbsRequestAndWork> getAll() {
        Session session = null;
        List requestAndWorks = new ArrayList<CrumbsRequestAndWork>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            requestAndWorks = session.createCriteria(CrumbsRequestAndWork.class).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return requestAndWorks;
    }

    @Override
    public Collection<CrumbsRequestAndWork> findByRequestID(CrumbsRequest request) {
        Session session = null;
        List requestAndWorks = new ArrayList<CrumbsRequestAndWork>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            //TODO
            //requestAndWorks = session.createCriteria(CrumbsRequestAndWork.class).add(Restrictions.eq("", request.getId())).add(Restrictions.eq("",).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return requestAndWorks;
    }

    @Override
    public Collection<CrumbsRequest> findByWorkId(int id) throws SQLException {
        Session session = null;
        ArrayList<CrumbsRequestAndWork> requestAndWorks = new ArrayList<CrumbsRequestAndWork>();
        ArrayList<CrumbsRequest> requests = new ArrayList<CrumbsRequest>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            requestAndWorks = (ArrayList<CrumbsRequestAndWork>) session.createCriteria(CrumbsRequestAndWork.class)
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
            for (CrumbsRequestAndWork rnv : requestAndWorks) {
                Hibernate.initialize(rnv.getCrumbsCatalogOfWork());
                if (rnv.getCrumbsCatalogOfWork().getId() == id) {
                    Hibernate.initialize(rnv.getCrumbsRequest());
                    requests.add(rnv.getCrumbsRequest());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return requests;
    }

    public boolean workIsFree(int id) throws SQLException {
        Session session = null;
        ArrayList<CrumbsRequestAndWork> requestAndWorks = new ArrayList<CrumbsRequestAndWork>();
        ArrayList<CrumbsRequest> requests = new ArrayList<CrumbsRequest>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            requestAndWorks = (ArrayList<CrumbsRequestAndWork>) session.createCriteria(CrumbsRequestAndWork.class)
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
            for (CrumbsRequestAndWork rnv : requestAndWorks) {
                Hibernate.initialize(rnv.getCrumbsCatalogOfWork());
                if (rnv.getCrumbsCatalogOfWork().getId() == id) {
                    Hibernate.initialize(rnv.getCrumbsRequest());
                    requests.add(rnv.getCrumbsRequest());
                }
            }
            for (CrumbsRequest request : requests) {
                Hibernate.initialize(request.getCrumbsRequestsState());
                if (request.getCrumbsRequestsState().getId() != 6) {
                    return false;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return true;
    }


    @Override
    public void delete(CrumbsRequestAndWork requestAndWork) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(requestAndWork);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

}

